﻿using System;
using System.Collections.Generic;
using UnityEngine;

public class NpcMgr : BaseManager<NpcMgr>
{
    public Dictionary<int,Npc> AllNpc = new Dictionary<int, Npc>();

    public static void OnCreateSenceNpcCmd(Cmd cmd)
    {
        if (!Client.CherkCmd(cmd, typeof(CreateSenceNpcCmd)))
            return;
        CreateSenceNpcCmd createNpc= cmd as CreateSenceNpcCmd;
        NpcTablebase npcDatabase = NpcTable.GetInstance()[createNpc.ModelID];
        if (npcDatabase == null)
        {
            Debug.LogError("未找到角色模型" + createNpc.ModelID);
        }
        //创建角色模型
        var npcObj = ResManager.GetInstance().load<GameObject>(npcDatabase.ModelPath);
        Npc npc = npcObj.AddComponent<Npc>();
        npc.Init(createNpc, npcDatabase);
        //把角色ID和角色脚本记录下来
        NpcMgr.GetInstance().AllNpc[npc.ThisId] = npc;
    }

    internal void Reset()
    {
        foreach(var npc in NpcMgr.GetInstance().AllNpc)
        {
            ResManager.GetInstance().Release(npc.Value.gameObject);
        }
        NpcMgr.GetInstance().AllNpc.Clear();
    }
}
